package cn.com.generaldata.os.service.indicator_evaluation.industry_collect_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业H指标计算类.
 */
public class MajorMarineIndustryHExponentCalculateService extends IndicatorEvaluationCalculateService {

	private String addValueOfMajorMarineIndustryName = ProvinceIndicatorConstant.MAJOR_MARINE_ADDVALUE_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = Double.valueOf(0);
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;

	}

	/**
	 * 计算省级的H指数
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = Double.valueOf("0");
		IndicatorAccountVO provinceVO = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		provinceVO.setAreaId(ProvinceIndicatorConstant.PROVICE_AREA_ID);
		Double provinceAddValueOfIndustry = getProvinceAddValueOfIndustry(provinceVO, indicator);

		for (String cityId : ProvinceIndicatorConstant.CITYS) {
			IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
			vo.setAreaId(cityId);

			Double cityAddValueOfIndustry = getCityAddValueOfIndustry(vo, indicator);

			if (cityAddValueOfIndustry == null) {
				continue;
			}
			result += Math.pow(cityAddValueOfIndustry / provinceAddValueOfIndustry, 2);
		}
		return result;
	}

	private Double getProvinceAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());

		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), industryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	private Double getCityAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), industryName);
		// Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

}
